const pre = 'SCENEKEY_';

const saveKey = (key) => {
  wx.setStorageSync(pre + key);
};

const isKeyUsed = (argKey) => {
  let keysObj = wx.getStorageInfoSync().keys;
  for (var i in keysObj) {
    if (keysObj.hasOwnProperty(i)) {
      let key = keysObj[i];
      if (key == pre + argKey) {
        return true;
      }
    }
  }
  return false;
};

const testKey = (key) => {
  console.log('uc-key', key)
  // 注: 如果不传key 这种场景认为永远是不可用的
  if (!key) return false;
  // 注: 如果传'ALWAYS' 这种场景认为永远是可用的
  if (key === 'ALWAYS') return true;
  let used = isKeyUsed(key);
  console.log('Storagekeys-before', wx.getStorageInfoSync().keys)
  console.log('used', used)
  if (!used) {
    saveKey(key);
  }
  console.log('Storagekeys-after', wx.getStorageInfoSync().keys)
  return !used;
};

module.exports = {
  saveKey,
  isKeyUsed,
  testKey
};
